home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1108 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  75 lines

  1. Path: ocbbs.gen.nz!not-for-mail
  2. From: steve@hn.ocbbs.gen.nz (Steve Detoni)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy commands - copy Binary files.
  5. Date: 10 Jan 1996 00:21:09 +1300
  6. Message-ID: <4ctj35$8ia@hn.ocbbs.gen.nz>
  7. References: <peterf.54.000F9B13@gears.efn.org>
  8. NNTP-Posting-Host: hn.hn.planet.gen.nz
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Peter F. (peterf@gears.efn.org) wrote:
  12. : Dear Friends,
  13. Hi there,
  14.  
  15. : I have been working on a c++ program, where the goal is to imitate the dos 
  16. : copy command.  I have been writing this program, and it works eccept for one 
  17. : small thing.  When I read a file, say 29,300 bytes and then want to output 
  18. : that file to a new directory, the files always come out 1 byte larger (ex. 
  19. : 29,301).  I dont know where the extra byte is coming from? Here is the code 
  20. : and maybe you could find the problem....
  21. Hmm, yeah, sounds like DOS is putting a EOF char (cntl Z) after your 
  22. close your file... Simple thing to fix!
  23.  
  24. // Simple and fast copy program!
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27.  
  28. const int BUFFSIZE = 4096; // 4k!
  29. char Buffer[BUFFSIZE];
  30.  
  31. int main (int argc, char** argv)
  32. {
  33.     int inFile, outFile;
  34.     int readSize;
  35.  
  36.     // parameters from command line
  37.     switch (argc)
  38.     {
  39.              case 2:
  40.                  inFile = open (argv[1], O_RDONLY | O_BINARY); 
  41.                  outFile = open (argv[2], O_CREAT);
  42.                  break;
  43.  
  44.              default:
  45.                  printf ("Usage: %s <inputfile> <outputfile>", argv[0]);
  46.                  return -1;
  47.               
  48.     }
  49.     if (inFile < 0)
  50.     {
  51.        printf ("Error opening %s", argv[1]);
  52.        close (outFile);
  53.        return -2;  
  54.     } 
  55.     if (outFile < 0)
  56.     {
  57.        printf ("Error opening %s", argv[2]);
  58.        close (inFile);
  59.        return -3;
  60.     }
  61.     // Copy process !
  62.     while ((readSize = read (inFile, Buffer, BUFFSIZE) > 0)
  63.     {
  64.           write (outFile, Buffer, readSize);
  65.     }
  66.     close (inFile);
  67.     close (outFile);
  68.     return 0;
  69. }
  70.  
  71. // Program untested, but should work ... <coded from memory>
  72.  
  73. // Should compile
  74. Steve.
  75.